<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./css/style.css">
    <!--引入Vue-->
    <script src="./js/axios.min.js"></script>
    <script src="./js/main.js"></script>
    <script src="./js/vue.js"></script>
    <title>详情页</title>
    <style>
        .feng1 {
            background-image: url(./img/fan.png);
            background-size: 100%;
            min-width: calc(80vh / 1872 * 1310);
            width: calc(80vh / 1872 * 1310);
            height: 80vh;
            position: relative;
            color: #000;
        }

        .fan1 {
            background-image: url(./img/fan-2.png);
            width: calc(80vh / 1872 * 942);
            height: calc(80vh / 1872 * 927);
            background-size: 100%;

            position: absolute;
            top: calc(80vh / 1872 * 202)
                /*226px*/
            ;
            left: calc(80vh / 1872 * 147)
                /*26px*/
            ;
            transform-origin: 52.52% 49.087%;
            animation: rotation 0.01s linear infinite;
        }

        .Open1 {
            background-image: url(./img/Open.png);
            width: calc(80vh / 1872 * 80)
                /*80px*/
            ;
            height: calc(80vh / 1872 * 80)
                /*80px*/
            ;
            background-size: 100%;

            position: absolute;
            top: calc(80vh / 1872 * 1419)
                /*50px*/
            ;
            left: calc(80vh / 1872 * 617)
                /*340px*/
            ;
            transform-origin: center;
            animation: rotation 0.01s linear infinit
        }

        .Open1:hover {
            background-image: url(./img/Open-2.png);
        }

        .feng2 {
            background-image: url(./img/feng.png);
            background-size: 100%;
            min-width: calc(80vh / 781.971947 *500);
            width: calc(80vh / 781.971947 *500);
            height: 80vh;
            /* 500 / 781.971947*/
            position: relative;
        }

        .fan2 {
            background-image: url(./img/feng-1.png);
            width: calc(80vh / 781.971947 * 200);
            height: calc(80vh / 781.971947 * 205);
            background-size: 100%;

            position: absolute;
            top: calc(80vh / 781.971947 * 226);
            left: calc(80vh / 781.971947 * 26);
            transform-origin: 47.52% 49.087%;
            animation: rotation 0.01s linear infinite;
        }

        .Open2 {
            background-image: url(./img/Open.png);
            width: calc(80vh / 781.971947 * 80)
                /*80px*/
            ;
            height: calc(80vh / 781.971947 * 80);
            background-size: 100%;

            position: absolute;
            top: calc(80vh / 781.971947 * 50);
            left: calc(80vh / 781.971947 * 340);
            transform-origin: center;
            animation: rotation 0.01s linear infinit
        }

        .Open2:hover {
            background-image: url(./img/Open-2.png);
        }

        .nr {
            width: 80%;
            margin: 5vh auto;
        }

        .windows {
            position: relative;
            top: 50%;
            transform: translateY(-50%);
            background-color: white;
            border-radius: 20px;
            padding: 50px 20px;
            width: 500px;
            min-height: 400px;
            text-align: center;
        }

        .windows>div>div {
            font-size: 24px;
        }

        .windows>div {
            margin: 50px 0;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="nav flex-container-between">
            <div>
                <ul class=" flex-container-start">
                    <li><a href="./home.html">主页</a></li>
                    <li><a href="./recharge.html">充值</a></li>
                    <li><a href="./increase.html">添加</a></li>
                </ul>
            </div>
            <div>
                <ul class=" flex-container-end">
                    <li><a @click="exit()">退出登录</a></li>
                </ul>
            </div>
        </div>
        <div class="nr flex-container-between">
            <div style="position: relative;">
                <div v-bind:class="'fan'+fan_data.type_id"
                    v-bind:style="{animation : fan_data.status?'rotation-accelerate 0.01s linear infinite':'rotation-decelerate 2s linear forwards' }">
                </div>
                <div v-bind:class="'feng'+fan_data.type_id"></div>
                <span v-bind:class="'Open'+fan_data.type_id" @click="toggleRotation"></span>
            </div>
            <div>
                <div class="windows">
                    <div>
                        <h1>{{fan_data.name}}</h1>
                        <div>风扇型号：{{fan_data.type_name}}</div>
                        <div>风扇id:{{fan_data.id}}</div>
                        <div>风扇功率:{{fan_data.fan_power}}</div>
                        <div>状态:{{showState(fan_data.status)}}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                fan_data: {
                    type_id: 1,
                    type_name: "",
                    id: 1,
                    name: "",
                    fan_power: 1,
                    status: true,
                },
                uesr_data: {
                    username: '',
                    remain: 0,
                    power: 0,
                },

            },
            methods: {
                toggleRotation(event) {
                    if (this.uesr_data.remain <= 0) {
                        alert('没电了，请充值');
                    } else {
                        if (!this.fan_data.status) {
                            axios.post('/appliance/poweron', {
                                    "id": URLSearchParams(location.search).get('id')
                                })
                                .then((response) => {
                                    const responsedata = response.data
                                    // 服务器状态码
                                    if (responsedata.code != 0) {
                                        // 如果不为0，说明有错误, msg为云端错误信息
                                        alert(responsedata.msg);
                                        // 截断后面的运行
                                        return;
                                    }
                                })
                                .catch(function (error) {
                                    console.log(error);
                                });
                        } else {
                            axios.post('/appliance/poweroff', {
                                    "id": URLSearchParams(location.search).get('id')
                                })
                                .then((response) => {
                                    const responsedata = response.data
                                    // 服务器状态码
                                    if (responsedata.code != 0) {
                                        // 如果不为0，说明有错误, msg为云端错误信息
                                        alert(responsedata.msg);
                                        // 截断后面的运行
                                        return;
                                    }
                                })
                                .catch(function (error) {
                                    console.log(error);
                                });
                        }
                        this.fan_data.status = !this.fan_data.status;
                    }
                },
                showState(state) {
                    if (state)
                        return "开启"
                    return "关闭"
                },
                exit() {
                    axios.post('/user/logout')
                        .then((response) => {
                            const responsedata = response.data
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                return;
                            }
                            window.location.href = "./login.html";
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            },
            watch: {
                'fan_data.remain': {
                    handler(newValue) {
                        if (newValue < 0.0005) {
                            this.fan_data.status = !this.fan_data.status;
                        }
                    }
                }
            },

            beforeCreate() { //生命周期初始化前
                axios.get('/appliance/info' + location.search)
                    .then((response) => {
                        const responsedata = response.data
                        console.log(response)
                        // 服务器状态码
                        if (responsedata.code != 0) {
                            // 如果不为0，说明有错误, msg为云端错误信息
                            alert(responsedata.msg);
                            if (responsedata.code == 401)
                                window.location.href = "./login.html";
                            // 截断后面的运行
                            return;
                        }
                        // 下面的代码只有在服务器返回状态码为0时才会执行, 也就是说服务器返回正确的数据
                        this.fan_data = responsedata.data;

                    })
                    .catch((error) => {
                        console.log(error);
                    })
                axios.get('/user/info')
                    .then((response) => {
                        const responsedata = response.data
                        console.log(response)
                        // 服务器状态码
                        if (responsedata.code != 0) {
                            // 如果不为0，说明有错误, msg为云端错误信息
                            alert(responsedata.msg);
                            // 截断后面的运行
                            if (responsedata.code == 401)
                                window.location.href = "./login.html";
                            return;
                        }
                        // 下面的代码只有在服务器返回状态码为0时才会执行, 也就是说服务器返回正确的数据
                        this.uesr_data = responsedata.data;

                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            mounted() {
                setInterval(() => {
                    axios.get('/user/info')
                        .then((response) => {
                            const responsedata = response.data
                            console.log(response)
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                if (responsedata.code == 401)
                                    window.location.href = "./login.html";
                                return;
                            }
                            // 下面的代码只有在服务器返回状态码为0时才会执行, 也就是说服务器返回正确的数据
                            this.uesr_data = responsedata.data;

                        })
                        .catch((error) => {
                            console.log(error);
                        });
                }, 10000)
            }
        })
    </script>
</body>

</html>